import {request} from "../../request/index.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail:{}
  },
  goodsInfo:{},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // console.log(options);
    const goods_id = options.goodsid;
    request({
      url:'https://api-hmugo-web.itheima.net/api/public/v1/goods/detail',
      data:{goods_id:goods_id}
    }).then(function(res){
      // console.log(res);
      that.goodsInfo = res.data.message;
        that.setData({
          goodsDetail:{
            goods_id:res.data.message.goods_id,
            goods_name:res.data.message.goods_name,
            goods_introduce:res.data.message.goods_introduce,
            goods_price:res.data.message.goods_price,
            pics:res.data.message.pics,
          }
        })
    })
  },
  handleImage:function(e){
    // console.log(this.goodsInfo);
    const nowpicsbigurl = e.currentTarget.dataset.picsbigurl;
    const url = this.goodsInfo.pics.map(function(v){
        return v.pics_big_url
    });
    wx.previewImage({
      current: nowpicsbigurl, // 当前显示图片的http链接
      urls: url // 需要预览的图片http链接列表
    })
  },
  handleAddCart:function(){
    //1.获取缓存中购物车数据
    let cart = wx.getStorageSync('cart')||[];
    const that = this;
    let index = cart.findIndex(function(v){
      return (v.goods_id === that.goodsInfo.goods_id);
    })
    if(index === -1){
      this.goodsInfo.num=1;
      this.goodsInfo.isActive=true;
      cart.push(this.goodsInfo);
    }else{
      cart[index].num++;
    }
    wx.setStorageSync('cart', cart)
    wx.showToast({
      title: '加入购物车成功',
      icon: 'success',
      mask: true,
    })
  }
})